package com.blx.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.blx.entity.AUser;
import com.blx.model.constants.ResponseConstants;
import com.blx.model.constants.WebConstants;
import com.blx.model.request.LoginRequest;
import com.blx.model.request.RegisterRequest;
import com.blx.model.response.LoginResponse;
import com.blx.model.response.RegisterResponse;
import com.blx.service.AUserService;
import com.blx.utils.CookieUtil;

/**
 * ClassName:  AUserController. <br/>
 * Description:  AUserController 页面处理类 <br/>
 * @author wangwl
 * 2013-3-4 下午10:04:16 <br/>
 * @version 1.0
 */
@Controller
@RequestMapping("/auser")
public class AUserController extends BaseController{

	private final Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private AUserService aUserService;

	@ResponseBody
	@RequestMapping("login")
	public LoginResponse login(LoginRequest loginRequest,HttpServletRequest request,HttpServletResponse response,HttpSession session){
		LoginResponse loginResponse = new LoginResponse();
		try {
			Assert.notNull(loginRequest);
			String captcha = (String) session.getAttribute("CAPTCHA_SESSION_KEY");
			if((loginRequest.getVcodeStatus() == 1) && (captcha == null || captcha.equals(""))){
				loginResponse.setResponseCode(ResponseConstants.FAIL_PARAM_ERROR);
				loginResponse.setErrorMsg("captcha is missing");
				loginResponse.setStatus(0);
				return loginResponse;
			}
			if(loginRequest.getVcode() != null && !loginRequest.getVcode().equalsIgnoreCase(captcha)){
				loginResponse.setResponseCode(ResponseConstants.FAIL_PARAM_ERROR);
				loginResponse.setErrorMsg("captcha code error");
			}
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("passport", loginRequest.getPassport());
			map.put("password", loginRequest.getPassword());
			AUser user = aUserService.getAUserEntity(map);
			if(user != null){
				session.setAttribute("SESSION_USER", user);
			}
			try {
				if(loginRequest.getAutoLogin()){
					CookieUtil.addCookie(response, WebConstants.PASSPORT, loginRequest.getPassport(), null, WebConstants.COOKIE_AGE);
					CookieUtil.addCookie(response, WebConstants.PASSWORD, loginRequest.getPassword(),null, WebConstants.COOKIE_AGE);
				}else{
					CookieUtil.addCookie(response, WebConstants.PASSPORT, loginRequest.getPassport(), null,0);
					CookieUtil.addCookie(response, WebConstants.PASSWORD, loginRequest.getPassword(),null, 0);
				}
			} catch (Exception e) {
				e.printStackTrace();
				logger.error("",e);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return new LoginResponse();
	}
	
	@ResponseBody
	@RequestMapping("register")
	public RegisterResponse userRegister(@RequestBody RegisterRequest request){
		try {
			RegisterResponse response = aUserService.userRegister(request);
			return response;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("",e);
			return null;
		}
	}
}

